home *** CD-ROM | disk | FTP | other *** search
Text File | 1995-02-07 | 5.0 KB | 186 lines | [TEXT/MPS ] |
- (*
- NBPNameList([namePattern[,includeTypes]]) -- Return a list of NBP names which match namePattern. Do not
- return the zone name as a part of the names. If the namePattern parameter is not present, return all names
- in the local zone of type "ADSP". Do not return types as a part of the names unless the includeTypes
- parameter is present and non-empty.
-
- To compile and link this file using Macintosh Programmer's Workshop,
-
- pascal -w NBPNameList.p
- link -m ENTRYPOINT -o HyperCommands -rt XFCN=2766 -sn Main=NBPNameList ∂
- NBPNameList.p.o "{MPW}"Libraries:interface.o "{MPW}"Libraries:Libraries:HyperXLib.o
-
- © Copyright 1990 by Apple Computer, Inc.
-
- Initial coding 6/90 by Harry R. Chesley.
- *)
-
- {$R-}
-
- {$S NBPNameList } { Segment name must be the same as the command name. }
-
- unit DummyUnit;
-
- interface
-
- uses MemTypes, QuickDraw, OSIntf, ToolIntf, CTBUtils, FTIntf, CMIntf, TMIntf, CRMIntf, AppleTalk, HyperXCmd;
-
- procedure EntryPoint(paramPtr: XCmdPtr);
-
- implementation
-
- procedure NBPNameList(paramPtr: XCmdPtr); forward;
-
- procedure EntryPoint(paramPtr: XCmdPtr);
-
- begin
- NBPNameList(paramPtr);
- end;
-
- procedure NBPNameList(paramPtr: XCmdPtr);
-
- {$I CTBUtil.inc}
-
- const kNBPTimeOutVal = 8; { Re-try NBP PLookupName every 3 seconds. }
- kNBPRetryCount = 5; { For five times. }
- kMaxLookupNames = 200; { Maximum number of names to lookup. }
- kLookupBufferSize = kMaxLookupNames*(sizeof(EntityName)+sizeof(AddrBlock)+4);
- kReturn = 13;
-
- var i: integer;
- s: Str255;
- includeTypes: boolean;
- nameToLookup: EntityName;
- nameBuffer: array [1..100] of SignedByte;
- pBlock: MPPParamBlock;
- lookupBuf: Ptr;
- result: Handle;
- resultSize: longInt;
- name: EntityName;
- addr: AddrBlock;
- p: Ptr;
-
- procedure Fail(errMsg: Str255); { set theResult and quit }
- begin
- { Dispose any buffers we managed to allocate before failing. }
- if lookupBuf <> nil then DisposPtr(lookupBuf);
- if result <> nil then DisposHandle(result);
- paramPtr^.returnValue := PasToZero(paramPtr,errMsg);
- exit(NBPNameList);
- end;
-
- procedure NBPPack(en: EntityName; var s: Str255);
- { Pack an EntityName structure into a single string in the form "<object>:<type>@<zone>". }
-
- begin
- s := en.objStr;
- if (en.typeStr <> '=') and (en.typeStr <> '') then s := Concat(s,Concat(':',en.typeStr));
- if (en.zoneStr <> '*') and (en.zoneStr <> '') then s := Concat(s,Concat('@',en.zoneStr));
- end;
-
- procedure NBPUnpack(var s: Str255; var en: EntityName);
- { Unpack a string of the form "<object>:<type>@<zone>" into an EntityName structure. }
-
- var o, o2: integer;
-
- begin
- o := Pos(':',s);
- if o > 0 then
- begin
- en.objStr := Copy(s,1,o-1);
- o2 := Pos('@',s);
- if o2 > o then
- begin
- en.typeStr := Copy(s,o+1,o2-o-1);
- en.zoneStr := Copy(s,o2+1,length(s)-o2);
- end
- else
- begin
- en.typeStr := Copy(s,o+1,length(s)-o);
- en.zoneStr := '*';
- end;
- end
- else
- begin
- en.typeStr := '=';
- o := Pos('@',s);
- if o > 0 then
- begin
- en.objStr := Copy(s,1,o-1);
- en.zoneStr := Copy(s,o+1,length(s)-o);
- end
- else
- begin
- en.objStr := s;
- en.zoneStr := '*';
- end;
- end;
- end;
-
- begin
- lookupBuf := nil;
- result := nil;
-
- { Check the parameter count. }
- if paramPtr^.paramCount > 2 then Fail('Invalid parameter count');
-
- { Load AppleTalk. }
- FailOSErr(OpenDriver('.MPP', i));
-
- { Get the name pattern. }
- if ParmPresent(1) then GetStrParm(1,s)
- else s := '=:ADSP';
- NBPUnpack(s,nameToLookup);
- NBPSetEntity(@nameBuffer,nameToLookup.objStr,nameToLookup.typeStr,nameToLookup.zoneStr);
- { Decide whether to include type information. }
- includeTypes := ParmPresent(2);
-
- { Prepare the name lookup request. }
- lookupBuf := NewPtr(kLookupBufferSize);
- if lookupBuf = nil then Fail('Out of memory');
- with pBlock do
- begin
- ioCompletion := nil;
- interval := kNBPTimeOutVal;
- count := kNBPRetryCount;
- entityPtr := @nameBuffer;
- retBuffPtr := lookupBuf;
- retBuffSize := kLookupBufferSize;
- maxToGet := kMaxLookupNames;
- numGotten := 0;
- end;
- FailOSErr(PLookupName(@pBlock,false));
-
- { Pry the names out of the lookup buffer. }
- result := NewHandle(0);
- resultSize := 0;
- if result = nil then Fail('out of memory');
- for i := 1 to pBlock.numGotten do
- begin
- if NBPExtract(lookupBuf,pBlock.numGotten,i,name,addr) <> noErr then Fail('Name extraction error');
- if not includeTypes then name.typeStr := '';
- name.zoneStr := '';
- NBPPack(name,s);
- resultSize := resultSize + length(s) + 1;
- SetHandleSize(result,resultSize);
- if MemError <> noErr then Fail('Out of memory');
- BlockMove(Ptr(ord4(@s)+1),pointer(ord4(result^)+resultSize-length(s)-1),length(s));
- p := pointer(ord4(result^)+resultSize-1);
- p^ := kReturn;
- end;
-
- { Get rid of the lookup buffer. }
- DisposPtr(lookupBuf);
-
- { Terminate and return the result. }
- if resultSize > 0 then
- begin
- p := pointer(ord4(result^)+resultSize-1);
- p^ := 0;
- paramPtr^.returnValue := result;
- end
- else DisposHandle(result);
- end;
-
- end.
-